package com.fzj.example.socket.tcp.heartbeat;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * ClassName: TcpClent
 * date: 2019/11/15 15:43
 * Description: TODO
 *
 * @author fanzj@bonree.com
 * @since JDK 1.8
 */
public class TcpClient {
    public static void main(String[] args) {

        ExecutorService executorService= Executors.newFixedThreadPool(10);
        Runnable runnable = () -> {
            try {
                Socket socket = new Socket(InetAddress.getByName("192.168.0.23"), 8080);
                socket.setKeepAlive(true);
                socket.setSoTimeout(10);
                socket.setOOBInline(true);
                int i = 0;
                while (true) {
                    System.out.println(Thread.currentThread().getName()+"发送心跳" + i + "次");
                    socket.sendUrgentData(0X1F);
                    System.out.println("现在处于连接状态" + i + "次");
                    Thread.sleep(1000);
                    i++;
                }
            }catch (IOException | InterruptedException e){
                e.printStackTrace();
            }
        };
        executorService.execute(runnable);
        executorService.execute(runnable);
        executorService.execute(runnable);
        executorService.execute(runnable);
        executorService.execute(runnable);
    }
}
